//
//  TradeBuyView.m
//  BTCoin
//
//  Created by Robin on 29/03/2018.
//  Copyright © 2018 Robin. All rights reserved.
//

#import "TradeBuyView.h"
#import "UIButton+Extension.h"
#import "TradePriceSocketModel.h"

#define kBuyCellHeight 30

#define kBuyButtonStyleTag  10001
@interface PriceView : UIView
@property (nonatomic,strong) UILabel * priceLabel;
@property (nonatomic,strong) UILabel * amountLabel;
@property (nonatomic,copy) NSString * price;
@property (nonatomic,copy) NSString * amount;
@property (nonatomic,copy) CommonStringBlock block;

- (instancetype)initWithTitle:(NSString *)title;
- (void)updateWith:(NSString *)price amount:(NSString *)amount;
@end


@interface TradeBuyView ()    

@property (nonatomic, strong) UILabel * totalNumLabel;
@property (nonatomic, strong) UILabel * tradeNumLabel;
@property (nonatomic, strong) UIButton * buyBtn;
@property (nonatomic, strong) UILabel * lastPriceNum;
@property (nonatomic, strong) UIView * priceContainerView;

@end

@implementation TradeBuyView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        
        UIScrollView * scrollView = [[UIScrollView alloc] init];
        [self addSubview:scrollView];
        [scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.leading.bottom.trailing.equalTo(self);
        }];
        
        UIView * scrollBackView = [[UIView alloc] init];
        [scrollView addSubview:scrollBackView];
        [scrollBackView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.leading.bottom.trailing.equalTo(scrollView);
            make.height.mas_equalTo(700);
            make.width.mas_equalTo(RectWidth(frame));
        }];
        
        UIView * priceSettingView = [[UIView alloc] init];
        [scrollView addSubview:priceSettingView];
        [priceSettingView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.leading.equalTo(scrollView);
            make.height.mas_equalTo(300);
        }];
        
        UIView * liveView = [[UIView alloc] init];
        liveView.backgroundColor = [UIColor clearColor];
        [scrollView addSubview:liveView];
        [liveView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.trailing.equalTo(scrollView);
            make.leading.equalTo(priceSettingView.mas_trailing).with.offset(18);
            make.width.equalTo(priceSettingView).multipliedBy(0.6);
        }];
        
        [self initPriceSettingView:priceSettingView];
        [self initLiveView:liveView];
        
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(listenToLogin:) name:LISTENTOAPPLOGINNOTIFICATION object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textFieldTextDidChange:) name:UITextFieldTextDidChangeNotification object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateTrendPrice) name:TRADEPRICESOCKETNOTIFICATION object:nil];
    }
    
    return self;
}

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)initPriceSettingView:(UIView *)containerView {
    //选择买入币
    UIView * coinTypeView = [[UIView alloc] init];
    coinTypeView.layer.borderWidth = 1.0;
    coinTypeView.layer.borderColor = UIColorFromRGB(0xE3E6E7).CGColor;
    [containerView addSubview:coinTypeView];
    [coinTypeView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.leading.equalTo(containerView).offset(10);
        make.trailing.equalTo(containerView).offset(-5);
        make.height.mas_equalTo(40);
    }];
    
    UIImageView * arrowImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"trade_arrow_down"]];
    [coinTypeView addSubview:arrowImageView];
    [arrowImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(coinTypeView);
        make.trailing.equalTo(coinTypeView).offset(-20);
        make.width.mas_equalTo(8);
        make.height.mas_equalTo(6);
    }];
    
    UIButton * typeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    typeBtn.backgroundColor = [UIColor clearColor];
    typeBtn.tag = kBuyButtonStyleTag;
    [typeBtn addTarget:self action:@selector(showTradeTypeAction:) forControlEvents:UIControlEventTouchUpInside];
    [coinTypeView addSubview:typeBtn];
    [typeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.trailing.equalTo(coinTypeView);
        make.width.mas_equalTo(50);
    }];
    
    UIView * lineView = [[UIView alloc] init];
    lineView.backgroundColor = UIColorFromRGB(0xE3E6E7);
    [coinTypeView addSubview:lineView];
    [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.equalTo(coinTypeView);
        make.width.mas_equalTo(1.0);
        make.trailing.equalTo(typeBtn.mas_leading);
    }];
    
    self.regionLabel = [[UILabel alloc] init];
    self.regionLabel.textColor = color_333333;
    self.regionLabel.font = [UIFont systemFontOfSize:16];
    self.regionLabel.text = @"CNY(USDT)";
    self.regionLabel.textAlignment = NSTextAlignmentCenter;
    [coinTypeView addSubview:self.regionLabel];
    [self.regionLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(coinTypeView);
        make.leading.equalTo(coinTypeView).offset(10);
        make.trailing.equalTo(lineView.mas_leading).offset(-3);
    }];
    
    //选择买入类型
    UIView * coinType = [[UIView alloc] init];
    coinType.layer.borderWidth = 1.0;
    coinType.layer.borderColor = UIColorFromRGB(0xE3E6E7).CGColor;
    [containerView addSubview:coinType];
    [coinType mas_makeConstraints:^(MASConstraintMaker *make) {
        make.trailing.leading.equalTo(coinTypeView);
        make.top.equalTo(coinTypeView.mas_bottom).with.offset(30);
        make.height.mas_equalTo(40);
    }];
    
    UIImageView * arrowImageStyle = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"trade_arrow_down"]];
    [coinTypeView addSubview:arrowImageStyle];
    [arrowImageStyle mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(coinType);
        make.trailing.equalTo(coinType).offset(-20);
        make.width.mas_equalTo(8);
        make.height.mas_equalTo(6);
    }];
    
    UIButton * typeSelectBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    typeSelectBtn.backgroundColor = [UIColor clearColor];
    typeSelectBtn.tag = kBuyButtonStyleTag + 1;
    [typeSelectBtn addTarget:self action:@selector(showTradeTypeAction:) forControlEvents:UIControlEventTouchUpInside];
    [coinType addSubview:typeSelectBtn];
    [typeSelectBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.trailing.equalTo(coinType);
        make.width.mas_equalTo(50);
    }];
    
    UIView * lineViewSecond = [[UIView alloc] init];
    lineViewSecond.backgroundColor = UIColorFromRGB(0xE3E6E7);
    [coinType addSubview:lineViewSecond];
    [lineViewSecond mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.equalTo(coinType);
        make.width.mas_equalTo(1.0);
        make.trailing.equalTo(typeSelectBtn.mas_leading);
    }];
    
    self.regionStyle = [[UILabel alloc] init];
    self.regionStyle.textColor = color_333333;
    self.regionStyle.font = [UIFont systemFontOfSize:16];
    self.regionStyle.text = @"限价交易";
    self.regionStyle.textAlignment = NSTextAlignmentCenter;
    [coinType addSubview:self.regionStyle];
    [self.regionStyle mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(coinType);
        make.leading.equalTo(coinType).offset(10);
        make.trailing.equalTo(lineViewSecond.mas_leading).offset(-3);
    }];
    
    //买入价格
    UIView * priceView = [[UIView alloc] init];
    priceView.layer.borderWidth = 1.0;
    priceView.layer.borderColor = UIColorFromRGB(0xE3E6E7).CGColor;
    [containerView addSubview:priceView];
    [priceView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(coinType.mas_bottom).offset(30);
        make.leading.trailing.height.equalTo(coinTypeView);
    }];
    
    self.priceTextField = [[UITextField alloc] init];
    self.priceTextField.textColor = color_333333;
    self.priceTextField.placeholder = @"价格";
    self.priceTextField.borderStyle = UITextBorderStyleNone;
    self.priceTextField.keyboardType = UIKeyboardTypeDecimalPad;
    [priceView addSubview:self.priceTextField];
    [self.priceTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(priceView);
        make.leading.equalTo(priceView).offset(10);
        make.trailing.equalTo(priceView);
    }];
    
    //买入数量
    UIView * numberView = [[UIView alloc] init];
    numberView.layer.borderWidth = 1.0;
    numberView.layer.borderColor = UIColorFromRGB(0xE3E6E7).CGColor;
    [containerView addSubview:numberView];
    [numberView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(priceView.mas_bottom).offset(30);
        make.leading.trailing.height.equalTo(priceView);
    }];
    
    self.numberTextField = [[UITextField alloc] init];
    self.numberTextField.textColor = color_333333;
    self.numberTextField.borderStyle = UITextBorderStyleNone;
    self.numberTextField.keyboardType = UIKeyboardTypeNumberPad;
    self.numberTextField.placeholder = @"数量";
    [numberView addSubview:self.numberTextField];
    [self.numberTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(numberView);
        make.leading.equalTo(numberView).offset(10);
        make.trailing.equalTo(numberView).offset(-60);
    }];
    
    self.typeLabel = [[UILabel alloc] init];
    self.typeLabel.textColor = color_333333;
    self.typeLabel.font = [UIFont systemFontOfSize:16];
    self.typeLabel.text = @"";
    [numberView addSubview:self.typeLabel];
    [self.typeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(numberView);
        make.trailing.equalTo(numberView).offset(-3);
    }];
    
    //可用
    UIView * totalView = [[UIView alloc] init];
    [containerView addSubview:totalView];
    [totalView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(numberView.mas_bottom).offset(10);
        make.leading.trailing.height.equalTo(priceView);
    }];
    
    UILabel * totalTitle = [[UILabel alloc] init];
    totalTitle.textColor = [UIColor colorWithHex:@"#999999"];
    totalTitle.font = [UIFont boldSystemFontOfSize:15];
    totalTitle.text = @"可用";
    [totalView addSubview:totalTitle];
    [totalTitle mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(totalView);
        make.leading.equalTo(totalView);
    }];
    
    self.totalNumLabel = [[UILabel alloc] init];
    self.totalNumLabel.textColor = [UIColor colorWithHex:@"#999999"];
    self.totalNumLabel.font = [UIFont systemFontOfSize:15];
    self.totalNumLabel.text = @"0.0000";
    [numberView addSubview:self.totalNumLabel];
    [self.totalNumLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(totalView);
        make.leading.equalTo(totalTitle.mas_trailing).offset(5);
    }];
    
    //创建滑动条对象
    UISlider *slider = [[UISlider alloc]init];
    
    //设置滑动条最大值
    slider.maximumValue=100;
    
    //设置滑动条的最小值，可以为负值
    slider.minimumValue=0;
    
    //设置滑动条的滑块位置float值
    slider.value=0;
    
    //左侧滑条背景颜色
    slider.minimumTrackTintColor=[UIColor redColor];
    
    //右侧滑条背景颜色
    slider.maximumTrackTintColor=[UIColor blueColor];
    
    //设置滑块的颜色
    slider.thumbTintColor=[UIColor blackColor];
    
    slider.continuous = YES;
    //对滑动条添加事件函数
    [slider addTarget:self action:@selector(pressSlider) forControlEvents:UIControlEventValueChanged];
    
    [numberView addSubview:slider];
    
    [slider mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(totalView);
        make.top.mas_equalTo(totalView.mas_bottom).with.offset(10);
    }];
    //交易额
    UIView * tradeView = [[UIView alloc] init];
    [containerView addSubview:tradeView];
    [tradeView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(slider.mas_bottom).offset(20);
        make.leading.trailing.height.equalTo(priceView);
    }];
    
    UILabel * tradeTitle = [[UILabel alloc] init];
    tradeTitle.textColor = [UIColor colorWithHex:@"#999999"];
    tradeTitle.font = [UIFont boldSystemFontOfSize:18];
    tradeTitle.text = @"交易额";
    [tradeView addSubview:tradeTitle];
    [tradeTitle mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(tradeView);
        make.leading.equalTo(tradeView);
    }];
    
    self.tradeNumLabel = [[UILabel alloc] init];
    self.tradeNumLabel.textColor = [UIColor colorWithHex:@"#333333"];
    self.tradeNumLabel.font = [UIFont systemFontOfSize:18];
    self.tradeNumLabel.text = @"0.0000";
    [numberView addSubview:self.tradeNumLabel];
    [self.tradeNumLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(tradeView);
        make.leading.equalTo(tradeTitle.mas_trailing).offset(5);
    }];
    
    UserInfo * uInfo = [UserInfo sharedUserInfo];
    self.buyBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.buyBtn setTitle:uInfo.bIsLogin?@"买入":@"登录" forState:UIControlStateNormal];
    [self.buyBtn setTitleColor:COLOR_Bg_White forState:UIControlStateNormal];
    [self.buyBtn setBackgroundImage:[UIImage imageWithColor:UIColorFromRGB(0xF4714A)] forState:UIControlStateNormal];
    [self.buyBtn addTarget:self action:@selector(buyAction:) forControlEvents:UIControlEventTouchUpInside];
    [containerView addSubview:self.buyBtn];
    [self.buyBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.equalTo(priceView);
        make.top.equalTo(tradeView.mas_bottom).with.offset(10);
        make.height.mas_equalTo(60);
    }];
}

- (void)initLiveView:(UIView *)containerView {
    //标题
    UIView * titleView = [[UIView alloc] init];
    [containerView addSubview:titleView];
    [titleView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.leading.trailing.equalTo(containerView);
        make.height.mas_equalTo(30);
    }];
    
    UILabel * trendTitle = [[UILabel alloc] init];
    trendTitle.textColor = [UIColor grayColor];
    trendTitle.font = [UIFont systemFontOfSize:11];
    trendTitle.textAlignment = NSTextAlignmentCenter;
    trendTitle.text = @"方向";
    [titleView addSubview:trendTitle];
    [trendTitle mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(titleView);
        make.leading.equalTo(titleView);
    }];
    
    UILabel * priceTitle = [[UILabel alloc] init];
    priceTitle.textColor = [UIColor grayColor];
    priceTitle.font = [UIFont systemFontOfSize:11];
    priceTitle.textAlignment = NSTextAlignmentCenter;
    priceTitle.text = @"价格";
    [titleView addSubview:priceTitle];
    [priceTitle mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(titleView);
        make.leading.equalTo(trendTitle.mas_trailing);
        make.width.equalTo(trendTitle);
    }];
    
    UILabel * amountTitle = [[UILabel alloc] init];
    amountTitle.textColor = [UIColor grayColor];
    amountTitle.font = [UIFont systemFontOfSize:11];
    amountTitle.textAlignment = NSTextAlignmentCenter;
    amountTitle.text = @"数量";
    [titleView addSubview:amountTitle];
    [amountTitle mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(titleView);
        make.leading.equalTo(priceTitle.mas_trailing);
        make.trailing.equalTo(titleView);
        make.width.equalTo(trendTitle);
    }];
    
    //实时价格
    _priceContainerView = [[UIView alloc] init];
    _priceContainerView.layer.borderWidth = 1.0;
    _priceContainerView.layer.borderColor = UIColorFromRGB(0x6A899C).CGColor;
    [containerView addSubview:_priceContainerView];
    [_priceContainerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(titleView.mas_bottom);
        make.leading.bottom.trailing.equalTo(containerView);
    }];
    
    UIView * lastContainerView = [[UIView alloc] init];
    [_priceContainerView addSubview:lastContainerView];
    [lastContainerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.leading.trailing.equalTo(_priceContainerView);
    }];
    
    UILabel * lastPriceTitle = [[UILabel alloc] init];
    lastPriceTitle.textColor = UIColorFromRGB(0x6A899C);
    lastPriceTitle.font = [UIFont systemFontOfSize:12];
    lastPriceTitle.textAlignment = NSTextAlignmentCenter;
    lastPriceTitle.text = @"最新价";
    [lastContainerView addSubview:lastPriceTitle];
    [lastPriceTitle mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(lastContainerView).offset(5);
        make.bottom.equalTo(lastContainerView).offset(-5);
        make.leading.equalTo(lastContainerView);
    }];
    
    _lastPriceNum = [[UILabel alloc] init];
    _lastPriceNum.textColor = UIColorFromRGB(0x6A899C);
    _lastPriceNum.font = [UIFont systemFontOfSize:12];
    _lastPriceNum.textAlignment = NSTextAlignmentCenter;
    _lastPriceNum.numberOfLines = 0;
    _lastPriceNum.text = @"--";
    [lastContainerView addSubview:_lastPriceNum];
    [_lastPriceNum mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(lastContainerView).offset(5);
        make.bottom.equalTo(lastContainerView).offset(-5);
        make.trailing.equalTo(lastContainerView);
        make.leading.equalTo(lastPriceTitle.mas_trailing).offset(3);
        make.width.equalTo(lastPriceTitle);
    }];
    
    UIView * lineView = [[UIView alloc] init];
    lineView.backgroundColor = UIColorFromRGB(0xE3E6E7);
    [_priceContainerView addSubview:lineView];
    [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.equalTo(_priceContainerView);
        make.top.equalTo(lastContainerView.mas_bottom);
        make.height.mas_equalTo(1.0);
    }];
    
    @WeakObj(self)
    PriceView * sellView7 = [[PriceView alloc] initWithTitle:@"卖7:"];
    sellView7.tag = 1;
    [_priceContainerView addSubview:sellView7];
    sellView7.block = ^(NSString *stringValue) {
        [selfWeak selectPrice:stringValue];
    };
    [sellView7 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.equalTo(_priceContainerView);
        make.top.equalTo(lineView.mas_bottom).offset(3);
        make.height.mas_equalTo(kBuyCellHeight);
    }];
    
    PriceView * sellView6 = [[PriceView alloc] initWithTitle:@"卖6:"];
    sellView6.tag = 2;
    [_priceContainerView addSubview:sellView6];
    sellView6.block = ^(NSString *stringValue) {
        [selfWeak selectPrice:stringValue];
    };
    [sellView6 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.equalTo(_priceContainerView);
        make.top.equalTo(sellView7.mas_bottom).offset(3);
        make.height.mas_equalTo(kBuyCellHeight);
    }];
    
    PriceView * sellView5 = [[PriceView alloc] initWithTitle:@"卖5:"];
    sellView5.tag = 3;
    [_priceContainerView addSubview:sellView5];
    sellView5.block = ^(NSString *stringValue) {
        [selfWeak selectPrice:stringValue];
    };
    [sellView5 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.equalTo(_priceContainerView);
        make.top.equalTo(sellView6.mas_bottom).offset(3);
        make.height.mas_equalTo(kBuyCellHeight);
    }];
    
    PriceView * sellView4 = [[PriceView alloc] initWithTitle:@"卖4:"];
    sellView4.tag = 4;
    [_priceContainerView addSubview:sellView4];
    sellView4.block = ^(NSString *stringValue) {
        [selfWeak selectPrice:stringValue];
    };
    [sellView4 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.equalTo(_priceContainerView);
        make.top.equalTo(sellView5.mas_bottom).offset(3);
        make.height.mas_equalTo(kBuyCellHeight);
    }];
    
    PriceView * sellView3 = [[PriceView alloc] initWithTitle:@"卖3:"];
    sellView3.tag = 5;
    [_priceContainerView addSubview:sellView3];
    sellView3.block = ^(NSString *stringValue) {
        [selfWeak selectPrice:stringValue];
    };
    [sellView3 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.equalTo(_priceContainerView);
        make.top.equalTo(sellView4.mas_bottom).offset(3);
        make.height.mas_equalTo(kBuyCellHeight);
    }];
    
    PriceView * sellView2 = [[PriceView alloc] initWithTitle:@"卖2:"];
    sellView2.tag = 6;
    [_priceContainerView addSubview:sellView2];
    sellView2.block = ^(NSString *stringValue) {
        [selfWeak selectPrice:stringValue];
    };
    [sellView2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.equalTo(_priceContainerView);
        make.top.equalTo(sellView3.mas_bottom).offset(3);
        make.height.mas_equalTo(kBuyCellHeight);
    }];
    
    PriceView * sellView1 = [[PriceView alloc] initWithTitle:@"卖1:"];
    sellView1.tag = 7;
    [_priceContainerView addSubview:sellView1];
    sellView1.block = ^(NSString *stringValue) {
        [selfWeak selectPrice:stringValue];
    };
    [sellView1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.equalTo(_priceContainerView);
        make.top.equalTo(sellView2.mas_bottom).offset(3);
        make.height.mas_equalTo(kBuyCellHeight);
    }];
    
    lineView = [[UIView alloc] init];
    lineView.backgroundColor = UIColorFromRGB(0xE3E6E7);
    [_priceContainerView addSubview:lineView];
    [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.equalTo(_priceContainerView);
        make.top.equalTo(sellView1.mas_bottom).offset(5);
        make.height.mas_equalTo(1.0);
    }];
    
    //买入
    PriceView * buyView1 = [[PriceView alloc] initWithTitle:@"买1:"];
    buyView1.tag = 11;
    [_priceContainerView addSubview:buyView1];
    buyView1.block = ^(NSString *stringValue) {
        [selfWeak selectPrice:stringValue];
    };
    [buyView1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.equalTo(_priceContainerView);
        make.top.equalTo(lineView.mas_bottom).offset(3);
        make.height.mas_equalTo(kBuyCellHeight);
    }];
    
    PriceView * buyView2 = [[PriceView alloc] initWithTitle:@"买2:"];
    buyView2.tag = 12;
    [_priceContainerView addSubview:buyView2];
    buyView2.block = ^(NSString *stringValue) {
        [selfWeak selectPrice:stringValue];
    };
    [buyView2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.equalTo(_priceContainerView);
        make.top.equalTo(buyView1.mas_bottom).offset(3);
        make.height.mas_equalTo(kBuyCellHeight);
    }];
    
    PriceView * buyView3 = [[PriceView alloc] initWithTitle:@"买3:"];
    buyView3.tag = 13;
    [_priceContainerView addSubview:buyView3];
    buyView3.block = ^(NSString *stringValue) {
        [selfWeak selectPrice:stringValue];
    };
    [buyView3 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.equalTo(_priceContainerView);
        make.top.equalTo(buyView2.mas_bottom).offset(3);
        make.height.mas_equalTo(kBuyCellHeight);
    }];
    
    PriceView * buyView4 = [[PriceView alloc] initWithTitle:@"买4:"];
    buyView4.tag = 14;
    [_priceContainerView addSubview:buyView4];
    buyView4.block = ^(NSString *stringValue) {
        [selfWeak selectPrice:stringValue];
    };
    [buyView4 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.equalTo(_priceContainerView);
        make.top.equalTo(buyView3.mas_bottom).offset(3);
        make.height.mas_equalTo(kBuyCellHeight);
    }];
    
    PriceView * buyView5 = [[PriceView alloc] initWithTitle:@"买5:"];
    buyView5.tag = 15;
    [_priceContainerView addSubview:buyView5];
    buyView5.block = ^(NSString *stringValue) {
        [selfWeak selectPrice:stringValue];
    };
    [buyView5 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.equalTo(_priceContainerView);
        make.top.equalTo(buyView4.mas_bottom).offset(3);
        make.height.mas_equalTo(kBuyCellHeight);
    }];
    
    PriceView * buyView6 = [[PriceView alloc] initWithTitle:@"买6:"];
    buyView6.tag = 16;
    [_priceContainerView addSubview:buyView6];
    buyView6.block = ^(NSString *stringValue) {
        [selfWeak selectPrice:stringValue];
    };
    [buyView6 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.equalTo(_priceContainerView);
        make.top.equalTo(buyView5.mas_bottom).offset(3);
        make.height.mas_equalTo(kBuyCellHeight);
    }];
    
    PriceView * buyView7 = [[PriceView alloc] initWithTitle:@"买7:"];
    buyView7.tag = 17;
    [_priceContainerView addSubview:buyView7];
    buyView7.block = ^(NSString *stringValue) {
        [selfWeak selectPrice:stringValue];
    };
    [buyView7 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.equalTo(_priceContainerView);
        make.top.equalTo(buyView6.mas_bottom).offset(3);
        make.bottom.equalTo(containerView).offset(-5);
        make.height.mas_equalTo(kBuyCellHeight);
    }];
}

#pragma mark - public methods
- (void)updateTypeView:(NSString *)type region:(NSString *)region {
    self.regionLabel.text = type;
    self.typeLabel.text = region;
    self.model.buyModel.type = type;
    self.model.buyModel.region = region;
}

#pragma mark - event response
- (void)priceOperationAction:(UIButton *)sender {
    if (1 == sender.tag) {
        NSLog(@"reduce");
    } else if (2 == sender.tag) {
        NSLog(@"add");
    }
}

- (void)showTradeTypeAction:(UIButton *)sender {
    if (sender.tag == kBuyButtonStyleTag) {
        if ([self.delegate respondsToSelector:@selector(showTradeTypeAction)]) {
            [self.delegate showTradeTypeAction];
        }
    }else if (sender.tag == kBuyButtonStyleTag + 1) {
        if ([self.delegate respondsToSelector:@selector(showTradeTypeStyleAction)]) {
            [self.delegate showTradeTypeStyleAction];
        }
    }
}

- (void)buyAction:(UIButton *)sender {
    if ([self.delegate respondsToSelector:@selector(buyAction)]) {
        [self.delegate buyAction];
    }
}

- (void)selectPrice:(NSString *)price {
    
}

#pragma mark - Notification
- (void)listenToLogin:(NSNotification *)sender {
    BOOL login = sender.object;
    [self.buyBtn setTitle:login?@"买入":@"登录" forState:UIControlStateNormal];
}

- (void)textFieldTextDidChange:(NSNotification *)sender {
    UITextField * textField = sender.object;
    if (textField == self.priceTextField) {
        self.model.buyModel.price = [textField.text doubleValue];
    } else if (textField == self.numberTextField) {
        self.model.buyModel.number = [textField.text integerValue];
    }
}

- (void)updateTrendPrice {
    TradePriceSocketModel * model = [TradePriceSocketModel sharedTradePriceSocketModel];
    if (model.sells.count == 7) {
        for (int i = 1; i <= 7; i++) {
            PriceView * pView = (PriceView*)[_priceContainerView viewWithTag:i];
            NSString * price = [NSString stringWithFormat:@"%@",model.sells[i-1][@"price"]];
            NSString * amount = [NSString stringWithFormat:@"%@",model.sells[i-1][@"volume"]];
            if ([model.sells[i-1][@"price"] doubleValue] == 0) {
                price = @"--";
                amount = @"--";
            }
            [pView updateWith:price amount:amount];
        }
    }
    
    if (model.buys.count == 7) {
        for (int i = 1; i <= 7; i++) {
            PriceView * pView = (PriceView*)[_priceContainerView viewWithTag:i+10];
            NSString * price = [NSString stringWithFormat:@"%@",model.buys[i-1][@"price"]];
            NSString * amount = [NSString stringWithFormat:@"%@",model.buys[i-1][@"volume"]];
            if ([model.buys[i-1][@"price"] doubleValue] == 0) {
                price = @"--";
                amount = @"--";
            }
            [pView updateWith:price amount:amount];
        }
    }
}

@end

@implementation PriceView

- (instancetype)initWithTitle:(NSString *)title {
    if (self == [super initWithFrame:CGRectZero]) {
        
        UILabel * titleLabel = [[UILabel alloc] init];
        if ([title containsString:@"买"]) {
            titleLabel.textColor = [UIColor greenColor];
        }else if ([title containsString:@"卖"]) {
            titleLabel.textColor = [UIColor redColor];
        }
        titleLabel.font = [UIFont systemFontOfSize:11];
        titleLabel.textAlignment = NSTextAlignmentLeft;
        titleLabel.text = title;
        [self addSubview:titleLabel];
        [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self);
            make.bottom.equalTo(self);
            make.leading.equalTo(self).offset(2);
            make.width.mas_equalTo(25);
        }];
        
        self.priceLabel = [[UILabel alloc] init];
        self.priceLabel.textColor = UIColorFromRGB(0x6A899C);
        self.priceLabel.font = [UIFont systemFontOfSize:10];
        self.priceLabel.textAlignment = NSTextAlignmentCenter;
        self.priceLabel.numberOfLines = 1;
        self.priceLabel.text = @"--";
        [self addSubview:self.priceLabel];
        [self.priceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.bottom.equalTo(self);
            make.leading.equalTo(titleLabel.mas_trailing).offset(2);
        }];
        
        self.amountLabel = [[UILabel alloc] init];
        self.amountLabel.textColor = UIColorFromRGB(0x6A899C);
        self.amountLabel.font = [UIFont systemFontOfSize:10];
        self.amountLabel.textAlignment = NSTextAlignmentCenter;
        self.amountLabel.numberOfLines = 1;
        self.amountLabel.text = @"--";
        [self addSubview:self.amountLabel];
        [self.amountLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.bottom.equalTo(self);
            make.leading.equalTo(self.priceLabel.mas_trailing).offset(5);
            make.trailing.equalTo(self).offset(-5);
            make.width.equalTo(self.priceLabel);
        }];
        
        UIButton * viewBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        viewBtn.backgroundColor = [UIColor clearColor];
        [viewBtn addTarget:self action:@selector(onClickAction:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:viewBtn];
        [viewBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.leading.bottom.trailing.equalTo(self);
        }];
    }
    
    return self;
}

- (void)updateWith:(NSString *)price amount:(NSString *)amount {
    self.priceLabel.text = price;
    self.amountLabel.text = amount;
}

- (void)onClickAction:(UIButton *)sender {
    if (self.block) {
        self.block(self.price);
    }
}

@end
